//ztree初始化
var setting = {
	view: {
		dblClickExpand: false,
		showLine: false,
		selectedMulti: false
	},
	data: {
		simpleData: {
			enable:true,
			idKey: "id",
			pIdKey: "pId",
			rootPId: ""
		}
	},
	callback: {
		beforeClick: function(treeId, treeNode) {
			var insertUrl=$('#ztreeList').attr('insertUrl');
			var updateUrl=$('#ztreeList').attr('updateUrl');
			var level=$('#ztreeList').attr('level');
			if(level==1){
				if (treeNode.isParent) {
					demoIframe.attr("src",insertUrl+"?id="+treeNode.id);
					return false;
				} else {
					if(treeNode.pId==''){
						demoIframe.attr("src",insertUrl+"?id="+treeNode.id);
					}else{
						demoIframe.attr("src",updateUrl+"?id="+treeNode.id);
					}
					return true;
				}
			}else if(level==2){
				if (treeNode.isParent) {    //如果是父节点。
					if(treeNode.level==0){
						demoIframe.attr("src",'');
					}else if(treeNode.level==1){
						demoIframe.attr("src",insertUrl+"?id="+treeNode.id);
					}
					return false;
				} else {
					if(treeNode.level==0){
						demoIframe.attr("src",'');
					}else if(treeNode.level==1){
						demoIframe.attr("src",insertUrl+"?id="+treeNode.id);
					}else if(treeNode.level==2){
						demoIframe.attr("src",updateUrl+"?id="+treeNode.id);
					}
					return true;
				}
			}else if(level==0){
				demoIframe.attr("src",updateUrl+"?id="+treeNode.id);
				return true;
			}
		}
	}
};

var zNodes =eval($('#ztreeList').val());
$(function(){
	var t = $("#treeDemo");
	$.fn.zTree.init($("#treeDemo"), setting, zNodes);
	demoIframe = $("#testIframe");
});


//ztree中的保存和修改方法
function operationdataZtree(dataurl,fromid){
	//判断必填文本框是否为空
	$(fromid).find('[datatype]').each(function(){
		if($(this).val().trim() == ""){
			var tishistr = $(this).addClass('Validform_error');
			$(this).parent().next().find('span').addClass('Validform_wrong').html('不能为空！');
		}
	});
	//判断文本框的内容是否填写正确
	var haserror = $(fromid).find('.Validform_error').length;
	if(haserror>0){
		alert('请填写正确数据！');
	}else{ 
		 $.post(dataurl,$(fromid).serialize(),function(result){
			 var status=result.msg.status;
			 var desc=result.msg.desc;
			    if(status == 0){
			    	layer.msg(desc, {icon: 1,time: 1000},
			    	 function(){
			    		//页面刷新
			    		parent.location.reload();
			    		parent.$('#testIframe').attr('src','');
			    	 }
			    	); 
			    
			    }else{
			    	layer.msg(desc, {icon: 0,time: 1000});
			    }
		  });
	}
}

//ztree中的删除方法
function deleteZtree(dataurl,fromid){
	//判断必填文本框是否为空
	$(fromid).find('[datatype]').each(function(){
		if($(this).val().trim() == ""){
			var tishistr = $(this).addClass('Validform_error');
			$(this).parent().next().find('span').addClass('Validform_wrong').html('不能为空！');
		}
	});
	//判断文本框的内容是否填写正确
	var haserror = $(fromid).find('.Validform_error').length;
	if(haserror>0){
		alert('请填写正确数据！');
	}else{ 
		 $.post(dataurl,$(fromid).serialize(),function(result){
			 var status=result.msg.status;
			 var desc=result.msg.desc;
			    if(status == 0){
			    	layer.msg(desc, {icon: 1,time: 1000},
			    	 function(){
			    		//页面刷新
			    		parent.location.reload();
			    		parent.$('#testIframe').attr('src','');
			    	 }
			    	); 
			    
			    }
		  });
	}
}